package xj.graph2d.shapes.uml;

import java.awt.geom.Point2D;
import java.util.Iterator;

import xj.graph2d.DrawShape;
import xj.graph2d.LineShape;
import xj.graph2d.RectangleShape;

public class LifeLineShape extends LineShape {

  public LifeLineShape(float x, float y1, float y2) {
    this(x, y1, y2, false);
  }

  public LifeLineShape(float x, float y1, float y2, boolean comp) {
    super(x, y1, x, y2, comp);
    setVertical(true);
    setAcceptSubshape(true);
  }

  public LifeLineShape() {
    this(false);
  }

  public LifeLineShape(boolean comp) {
    super(comp);
    setVertical(true);
    setAcceptSubshape(true);
  }

  @Override
  public DrawShape makeInstance() {
    return new LifeLineShape(fullCloneMode && comp != null);
  }

  @Override
  public void componentMoved(DrawShape comp, float dx, float dy) {
    layoutComponents();
  }

  @Override
  public float getWidthSensitivity() {
    return 10;
  }

  @Override
  public void layoutComponents() {

    // System.out.println("LifeLineShape.layoutComponents()");

    if (subshapes != null) {
      Iterator<DrawShape> iter = subshapes.iterator();
      if (iter != null) {
	while (iter.hasNext()) {
	  DrawShape s = iter.next();
	  if (s instanceof RectangleShape) {
	    // "Execution".equals(s.getTemplateName())) {
	    float x = (float) p1.getX();
	    float ymin = Math.min((float) p1.getY(), (float) p2.getY());
	    float ymax = Math.max((float) p1.getY(), (float) p2.getY());
	    float h = (float) s.getBounds().getHeight();
	    Point2D c = s.getCenterPosition();
	    float cy = (float) c.getY();
	    float y = cy;
	    if ((y + h / 2) > ymax) {
	      y = ymax - h / 2;
	    }
	    if ((y - h / 2) < ymin) {
	      y = ymin + h / 2;
	    }
	    s.setCenterPosition(x, y);
	  }
	}
      }
    }
  }

}
